{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d6104da9",
   "metadata": {},
   "source": [
    "# Python Folium: Create Web Maps From Your Data\n",
    "\n",
    "This notebook contains code associated with the Real Python tutorial [Python Folium: Create Web Maps From Your Data](https://realpython.com/python-folium-web-maps-from-data/). For additional information, please visit the tutorial link."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9df7d36",
   "metadata": {},
   "source": [
    "## Install Folium"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ba43aa2",
   "metadata": {},
   "source": [
    "```pscon\n",
    "PS> python -m venv venv\n",
    "PS> venv\\Scripts\\activate\n",
    "(venv) PS> python -m pip install folium pandas\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7e1ada9",
   "metadata": {},
   "source": [
    "```console\n",
    "$ python -m venv venv\n",
    "$ source venv/bin/activate\n",
    "(venv) $ python -m pip install folium pandas\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b041474",
   "metadata": {},
   "source": [
    "## Create and Style a Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "720d80ca",
   "metadata": {},
   "source": [
    "### Display a Web Tile---in Style!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "964c08ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "\n",
    "m = folium.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1471423",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "\n",
    "m = folium.Map(tiles=\"cartodb positron\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcd645c8",
   "metadata": {},
   "source": [
    "### Add a Geolocation and Adjust the Zoom Level"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3327be0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "\n",
    "m = folium.Map(location=(49.25, -123.12), tiles=\"cartodb positron\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8706ee72",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "\n",
    "m = folium.Map(location=(30, 10), zoom_start=3, tiles=\"cartodb positron\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6c46139",
   "metadata": {},
   "source": [
    "## Add a GeoJSON Countries Layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94bd3893",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "\n",
    "political_countries_url = (\n",
    "    \"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson\"\n",
    ")\n",
    "\n",
    "m = folium.Map(location=(30, 10), zoom_start=3, tiles=\"cartodb positron\")\n",
    "folium.GeoJson(political_countries_url).add_to(m)\n",
    "\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b3d85f5",
   "metadata": {},
   "source": [
    "## Create a Choropleth Map With Your Data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a986a1e",
   "metadata": {},
   "source": [
    "### Get Data on Ecological Footprint Per Country"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1265695c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "eco_footprints = pd.read_csv(\"footprint.csv\")\n",
    "\n",
    "eco_footprints.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6597294f",
   "metadata": {},
   "source": [
    "### Add the Data to Your Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6025cd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "import pandas as pd\n",
    "\n",
    "eco_footprints = pd.read_csv(\"footprint.csv\")\n",
    "political_countries_url = (\n",
    "    \"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson\"\n",
    ")\n",
    "\n",
    "m = folium.Map(location=(30, 10), zoom_start=3, tiles=\"cartodb positron\")\n",
    "folium.Choropleth(\n",
    "    geo_data=political_countries_url,\n",
    "    data=eco_footprints,\n",
    "    columns=[\"Country/region\", \"Ecological footprint\"],\n",
    "    key_on=\"feature.properties.name\",\n",
    ").add_to(m)\n",
    "\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82792ad1",
   "metadata": {},
   "source": [
    "## Style Your Folium Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcd32b18",
   "metadata": {},
   "source": [
    "### Adapt the Color Scheme and Opacity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2badbdbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "import pandas as pd\n",
    "\n",
    "eco_footprints = pd.read_csv(\"footprint.csv\")\n",
    "political_countries_url = (\n",
    "    \"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson\"\n",
    ")\n",
    "\n",
    "m = folium.Map(location=(30, 10), zoom_start=3, tiles=\"cartodb positron\")\n",
    "folium.Choropleth(\n",
    "    geo_data=political_countries_url,\n",
    "    data=eco_footprints,\n",
    "    columns=(\"Country/region\", \"Ecological footprint\"),\n",
    "    key_on=\"feature.properties.name\",\n",
    "    fill_color=\"RdYlGn_r\",\n",
    "    fill_opacity=0.8,\n",
    "    line_opacity=0.3,\n",
    "    nan_fill_color=\"white\",\n",
    "    legend_name=\"Ecological footprint per capita\",\n",
    ").add_to(m)\n",
    "\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18e05c5c",
   "metadata": {},
   "source": [
    "### Use Custom Data Binning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0b20351",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "import pandas as pd\n",
    "\n",
    "eco_footprints = pd.read_csv(\"footprint.csv\")\n",
    "max_eco_footprint = eco_footprints[\"Ecological footprint\"].max()\n",
    "political_countries_url = (\n",
    "    \"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson\"\n",
    ")\n",
    "\n",
    "m = folium.Map(location=(30, 10), zoom_start=3, tiles=\"cartodb positron\")\n",
    "folium.Choropleth(\n",
    "    geo_data=political_countries_url,\n",
    "    data=eco_footprints,\n",
    "    columns=(\"Country/region\", \"Ecological footprint\"),\n",
    "    key_on=\"feature.properties.name\",\n",
    "    bins=[0, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, max_eco_footprint],\n",
    "    fill_color=\"RdYlGn_r\",\n",
    "    fill_opacity=0.8,\n",
    "    line_opacity=0.3,\n",
    "    nan_fill_color=\"white\",\n",
    "    legend_name=\"Ecological footprint per capita\",\n",
    ").add_to(m)\n",
    "\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "442b3939",
   "metadata": {},
   "source": [
    "### Add a Layer Control Element"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ab29613",
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium\n",
    "import pandas as pd\n",
    "\n",
    "eco_footprints = pd.read_csv(\"footprint.csv\")\n",
    "max_eco_footprint = eco_footprints[\"Ecological footprint\"].max()\n",
    "political_countries_url = (\n",
    "    \"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson\"\n",
    ")\n",
    "\n",
    "m = folium.Map(location=(30, 10), zoom_start=3, tiles=\"cartodb positron\")\n",
    "folium.Choropleth(\n",
    "    geo_data=political_countries_url,\n",
    "    data=eco_footprints,\n",
    "    columns=(\"Country/region\", \"Ecological footprint\"),\n",
    "    key_on=\"feature.properties.name\",\n",
    "    bins=(0, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, max_eco_footprint),\n",
    "    fill_color=\"RdYlGn_r\",\n",
    "    fill_opacity=0.8,\n",
    "    line_opacity=0.3,\n",
    "    nan_fill_color=\"white\",\n",
    "    legend_name=\"Ecological footprint per capita\",\n",
    "    name=\"Countries by ecological footprint per capita\",\n",
    ").add_to(m)\n",
    "folium.LayerControl().add_to(m)\n",
    "\n",
    "m"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language": "python",
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "vscode": {
   "interpreter": {
    "hash": "b63e7ba5cc9b279df3379b3b9f397154573a6d2720f4c4db5ed59f7535d647b9"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
